/**
 * @file        done.ipp
 * @brief       Defines done items abstraction implementation.
 *
 * @author      Emre Turkay <emreturkay at gmail>
 * @date        Nov.2.2008
 *
 * Copyright Emre Turkay 2009
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file COPYRIGHT or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 */

namespace gtd
{

    inline
    done_item::done_item()
    {
    }

    inline
    done_item::done_item(const std::string& l)
        : time_(boost::posix_time::second_clock::local_time()),
          line_(l)
    {
    }

    inline
    done_item::done_item(boost::posix_time::ptime t, const std::string& line)
        : time_(t), line_(line)
    {
    }

    inline
    bool done_item::operator<(const done_item& i) const
    {
        if (time_ < i.time_)
            return true;
        if (time_ > i.time_)
            return false;
        return line_ > i.line_;
    }

    inline
    void done_item::write(std::ostream& os) const
    {
        boost::posix_time::time_facet* facet(
            new boost::posix_time::time_facet("[%a %Y-%b-%d %H:%M]"));
        os.imbue(std::locale(os.getloc(), facet));
        os << time_ << " " << line_ << std::endl;
    }

    inline
    void done_item::read(std::istream& is)
    {
        boost::posix_time::time_input_facet* facet(
            new boost::posix_time::time_input_facet("[%a %Y-%b-%d %H:%M]"));
        is.imbue(std::locale(is.getloc(), facet));

        is >> time_;
        std::getline(is, line_);
        while (isspace(line_[0]))
            line_.erase(line_.begin());
    }

    inline
    const boost::posix_time::ptime& done_item::time() const
    {
        return time_;
    }

    inline
    const std::string& done_item::line() const
    {
        return line_;
    }

} // namespace gtd
